package com.cysyz.web.modules.base.support.filter;

import com.cysyz.web.modules.base.support.SysConstants;
import com.cysyz.web.modules.base.user.domain.SysUser;
import com.cysyz.web.modules.base.user.service.SysUserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

/**
 * 如果当前session中没有用户，那么就自动进行登录。用于开发中，省去手动登录的麻烦。
 */
public class NoCacheFilter implements Filter {
    private String userName;
    private SysUserService sysUserService;
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		chain.doFilter(request, response);
        HttpServletResponse rep = (HttpServletResponse) response;
        rep.addHeader("Cache-Control", "no-store, must-revalidate");
        rep.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");


    }

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
        userName = filterConfig.getInitParameter("userName");
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
        sysUserService = (SysUserService)wac.getBean("sysUserService");
	}

}
